﻿<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>g2kml Tests</title>
    <link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/3.2.0/build/cssfonts/fonts-min.css" />
    <script type="text/javascript" src="http://yui.yahooapis.com/3.2.0/build/yui/yui-min.js"></script>
    <script type="text/javascript" src="../js/yuitest-core-min.js"></script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript" src="../js/g2kml.js"></script>
    <style type="text/css">
        .yui3-skin-sam .yui3-console-entry-pass .yui3-console-entry-cat
        {
            background-color: green;
            color: #fff;
        }
        
        .yui3-skin-sam .yui3-console-entry-fail .yui3-console-entry-cat
        {
            background-color: red;
            color: #fff;
        }
        
        .yui3-skin-sam .yui3-console-entry-ignore .yui3-console-entry-cat
        {
            background-color: #666;
        }
    </style>
    <body class="yui3-skin-sam  yui-skin-sam">
        <h1>
            g2kml</h1>
        <p>
            Tests</p>
        <div id="testLogger">
        </div>
        <script type="text/javascript">
            YUI().use("console", function (Y) {

                var isArray_TestCase = new YUITest.TestCase({
                    //--------------------------------
                    //Setup and TearDown
                    //--------------------------------
                    name: "isArray",
                    setUp: function () {
                        this.target = new g2kml();
                    },
                    tearDown: function () {
                        delete this.target;
                    },
                    _should: {
                        error: {
                    }
                },

                //---------------------------------
                //Tests
                //---------------------------------
                testNumber: function () {
                    result = this.target.isArray(17.37);
                    YUITest.Assert.areEqual(false, result);
                },
                testObjectLiteral: function () {
                    result = this.target.isArray({});
                    YUITest.Assert.areEqual(false, result);
                },
                testArrayLiteral: function () {
                    result = this.target.isArray([]);
                    YUITest.Assert.areEqual(true, result);
                },
                testArrayNew: function () {
                    result = this.target.isArray(new Array());
                    YUITest.Assert.areEqual(true, result);
                }
            });

            var getPointKmlGeometryString_TestCase = new YUITest.TestCase({
                //--------------------------------
                //Setup and TearDown
                //--------------------------------
                name: "getPointKmlGeometryString",
                setUp: function () {
                    this.target = new g2kml();
                },
                tearDown: function () {
                    delete this.target;
                },
                _should: {
                    error: {
                        "four arguments should fail": true,
                        "array with four items should fail": true,
                        "random object argument should fail": true
                    }
                },

                //---------------------------------
                //Tests
                //---------------------------------
                "lat and lng arguments should succeed": function () {
                    result = this.target.getPointKmlGeometryString(17.37, 78.47);
                    YUITest.Assert.areEqual("<Point><coordinates>78.47,17.37</coordinates></Point>", result);
                },
                "lat, lng and alt arguments should succeed": function () {
                    result = this.target.getPointKmlGeometryString(17.45, 78.55, 2.3);
                    YUITest.Assert.areEqual("<Point><coordinates>78.55,17.45,2.3</coordinates></Point>", result);
                },
                "four arguments should fail": function () {
                    result = this.target.getPointKmlGeometryString(17.56, 78.33, 4.5, 3.4);
                },
                "no arguments should return an empty string": function () {
                    result = this.target.getPointKmlGeometryString();
                    YUITest.Assert.areEqual("", result);
                },
                "array with lat and lng items should succeed": function () {
                    result = this.target.getPointKmlGeometryString([17.37, 78.47]);
                    YUITest.Assert.areEqual("<Point><coordinates>78.47,17.37</coordinates></Point>", result);
                },
                "array with lat, lng and alt items should succeed": function () {
                    result = this.target.getPointKmlGeometryString([17.45, 78.55, 2.3]);
                    YUITest.Assert.areEqual("<Point><coordinates>78.55,17.45,2.3</coordinates></Point>", result);
                },
                "array with four items should fail": function () {
                    result = this.target.getPointKmlGeometryString([17.56, 78.33, 4.5, 3.4]);
                },
                "google LatLng object argument should succeed": function () {
                    result = this.target.getPointKmlGeometryString(new google.maps.LatLng(17.37, 78.47));
					var pattern = new RegExp("<Point><coordinates>78.47.*,17.37.*</coordinates></Point>");
                    YUITest.Assert.areSame(true, pattern.test(result));
                },
                "random object argument should fail": function () {
                    result = this.target.getPointKmlGeometryString({ 'some': 78.47, "test": 17.37 });
                }
            });


            var getLineKmlGeometryString_TestCase = new YUITest.TestCase({
                //--------------------------------
                //Setup and TearDown
                //--------------------------------
                name: "getLineKmlGeometryString",
                setUp: function () {
                    this.target = new g2kml();
                },
                tearDown: function () {
                    delete this.target;
                },
                _should: {
                    error: {
                }
            },

            //---------------------------------
            //Tests
            //---------------------------------
            "MVCArray of LatLngs should succeed": function () {
                var ll1 = new google.maps.LatLng(17.37, 78.47);
                var ll2 = new google.maps.LatLng(17.55, 78.21);
                var ll3 = new google.maps.LatLng(17.15, 78.73);
                var data = new google.maps.MVCArray([ll1, ll2, ll3]);
                result = this.target.getLineKmlGeometryString(data);
				var pattern = new RegExp("<LineString><coordinates>78.47.*,17.37.* 78.21.*,17.55.* 78.73.*,17.15.*</coordinates></LineString>");
                YUITest.Assert.areSame(true, pattern.test(result));
            },
            "array of LatLngs should succeed": function () {
                var ll1 = new google.maps.LatLng(17.37, 78.47);
                var ll2 = new google.maps.LatLng(17.55, 78.21);
                var ll3 = new google.maps.LatLng(17.15, 78.73);
                var data = [ll1, ll2, ll3];
                result = this.target.getLineKmlGeometryString(data);
                var pattern = new RegExp("<LineString><coordinates>78.47.*,17.37.* 78.21.*,17.55.* 78.73.*,17.15.*</coordinates></LineString>");
                YUITest.Assert.areSame(true, pattern.test(result));
            },
            "getPath value from Polyline should succeed": function () {
                var ll1 = new google.maps.LatLng(17.37, 78.47);
                var ll2 = new google.maps.LatLng(17.55, 78.21);
                var ll3 = new google.maps.LatLng(17.15, 78.73);
                var data = new google.maps.Polyline({ 'path': [ll1, ll2, ll3] });
                result = this.target.getLineKmlGeometryString(data.getPath());
                var pattern = new RegExp("<LineString><coordinates>78.47.*,17.37.* 78.21.*,17.55.* 78.73.*,17.15.*</coordinates></LineString>");
                YUITest.Assert.areSame(true, pattern.test(result));
            }
        });

        var getPolygonKmlGeometryString_TestCase = new YUITest.TestCase({
            //--------------------------------
            //Setup and TearDown
            //--------------------------------
            name: "getPolygonKmlGeometryString",
            setUp: function () {
                this.target = new g2kml();
            },
            tearDown: function () {
                delete this.target;
            },
            _should: {
                error: {
            }
        },

        //---------------------------------
        //Tests
        //---------------------------------
        "an array of paths should succeed": function () {
            var ll1 = new google.maps.LatLng(17.37, 78.47);
            var ll2 = new google.maps.LatLng(17.55, 78.21);
            var ll3 = new google.maps.LatLng(17.15, 78.73);
            var ll4 = new google.maps.LatLng(17.35, 78.33);
            var ll5 = new google.maps.LatLng(17.25, 78.56);

            var hll1 = new google.maps.LatLng(17.47, 78.17);
            var hll2 = new google.maps.LatLng(17.35, 78.11);
            var hll3 = new google.maps.LatLng(17.55, 78.53);
            var hll4 = new google.maps.LatLng(17.25, 78.43);
            var hll5 = new google.maps.LatLng(17.65, 78.46);

            result = this.target.getPolygonKmlGeometryString([[ll1, ll2, ll3, ll4, ll5], [hll1, hll2, hll3, hll4, hll5]]);
			var pattern = new RegExp("<Polygon><outerBoundaryIs><LinearRing><coordinates>78.4.*,17.3.* 78.2.*,17.5.* 78.7.*,17.1.* 78.3.*,17.3.* 78.5.*,17.2.*</coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>78.1.*,17.4.* 78.1.*,17.3.* 78.5.*,17.5.* 78.4.*,17.2.* 78.4.*,17.6.*</coordinates></LinearRing></innerBoundaryIs></Polygon>");
            YUITest.Assert.areSame(true, pattern.test(result));
        },
        "an MVCArray of paths should succeed": function () {
            var ll1 = new google.maps.LatLng(17.37, 78.47);
            var ll2 = new google.maps.LatLng(17.55, 78.21);
            var ll3 = new google.maps.LatLng(17.15, 78.73);
            var ll4 = new google.maps.LatLng(17.35, 78.33);
            var ll5 = new google.maps.LatLng(17.25, 78.56);

            var hll1 = new google.maps.LatLng(17.47, 78.17);
            var hll2 = new google.maps.LatLng(17.35, 78.11);
            var hll3 = new google.maps.LatLng(17.55, 78.53);
            var hll4 = new google.maps.LatLng(17.25, 78.43);
            var hll5 = new google.maps.LatLng(17.65, 78.46);

            var mvcarr = new google.maps.MVCArray([[ll1, ll2, ll3, ll4, ll5], [hll1, hll2, hll3, hll4, hll5]]);
            result = this.target.getPolygonKmlGeometryString(mvcarr);
            var pattern = new RegExp("<Polygon><outerBoundaryIs><LinearRing><coordinates>78.4.*,17.3.* 78.2.*,17.5.* 78.7.*,17.1.* 78.3.*,17.3.* 78.5.*,17.2.*</coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>78.1.*,17.4.* 78.1.*,17.3.* 78.5.*,17.5.* 78.4.*,17.2.* 78.4.*,17.6.*</coordinates></LinearRing></innerBoundaryIs></Polygon>");
            YUITest.Assert.areSame(true, pattern.test(result));
        },
        "getPaths value from Polygon should succeed": function () {
            var ll1 = new google.maps.LatLng(17.37, 78.47);
            var ll2 = new google.maps.LatLng(17.55, 78.21);
            var ll3 = new google.maps.LatLng(17.15, 78.73);
            var ll4 = new google.maps.LatLng(17.35, 78.33);
            var ll5 = new google.maps.LatLng(17.25, 78.56);

            var hll1 = new google.maps.LatLng(17.47, 78.17);
            var hll2 = new google.maps.LatLng(17.35, 78.11);
            var hll3 = new google.maps.LatLng(17.55, 78.53);
            var hll4 = new google.maps.LatLng(17.25, 78.43);
            var hll5 = new google.maps.LatLng(17.65, 78.46);

            var poly = new google.maps.Polygon({ 'paths': [[ll1, ll2, ll3, ll4, ll5], [hll1, hll2, hll3, hll4, hll5]] });
            result = this.target.getPolygonKmlGeometryString(poly.getPaths());
            var pattern = new RegExp("<Polygon><outerBoundaryIs><LinearRing><coordinates>78.4.*,17.3.* 78.2.*,17.5.* 78.7.*,17.1.* 78.3.*,17.3.* 78.5.*,17.2.*</coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>78.1.*,17.4.* 78.1.*,17.3.* 78.5.*,17.5.* 78.4.*,17.2.* 78.4.*,17.6.*</coordinates></LinearRing></innerBoundaryIs></Polygon>");
            YUITest.Assert.areSame(true, pattern.test(result));
        }
    });

    var getOverlayKmlGeometryString_TestCase = new YUITest.TestCase({
        //--------------------------------
        //Setup and TearDown
        //--------------------------------
        name: "getOverlayKmlGeometryString",
        setUp: function () {
            this.target = new g2kml();
        },
        tearDown: function () {
            delete this.target;
        },
        _should: {
            error: {
        }
    },

    //---------------------------------
    //Tests
    //---------------------------------
    "marker argument should succeed": function () {
        var position = new google.maps.LatLng(17.37, 78.22);
        var marker = new google.maps.Marker({ 'position': position });
        result = this.target.getOverlayKmlGeometryString(marker);
        var pattern = new RegExp("<Point><coordinates>78.22.*,17.37.*</coordinates></Point>");
		YUITest.Assert.areSame(true, pattern.test(result));
    },
    "polyline argument should succeed": function () {
        var ll1 = new google.maps.LatLng(17.37, 78.47);
        var ll2 = new google.maps.LatLng(17.55, 78.21);
        var ll3 = new google.maps.LatLng(17.15, 78.73);
        var data = new google.maps.Polyline({ 'path': [ll1, ll2, ll3] });
        result = this.target.getOverlayKmlGeometryString(data);
        var pattern = new RegExp("<LineString><coordinates>78.47.*,17.37.* 78.21.*,17.55.* 78.73.*,17.15.*</coordinates></LineString>");
		YUITest.Assert.areSame(true, pattern.test(result));
    },
    "polygon argument should succeed": function () {
        var ll1 = new google.maps.LatLng(17.37, 78.47);
        var ll2 = new google.maps.LatLng(17.55, 78.21);
        var ll3 = new google.maps.LatLng(17.15, 78.73);
        var ll4 = new google.maps.LatLng(17.35, 78.33);
        var ll5 = new google.maps.LatLng(17.25, 78.56);

        var hll1 = new google.maps.LatLng(17.47, 78.17);
        var hll2 = new google.maps.LatLng(17.35, 78.11);
        var hll3 = new google.maps.LatLng(17.55, 78.53);
        var hll4 = new google.maps.LatLng(17.25, 78.43);
        var hll5 = new google.maps.LatLng(17.65, 78.46);

        var poly = new google.maps.Polygon({ 'paths': [[ll1, ll2, ll3, ll4, ll5], [hll1, hll2, hll3, hll4, hll5]] });
        result = this.target.getOverlayKmlGeometryString(poly);
        var pattern = new RegExp("<Polygon><outerBoundaryIs><LinearRing><coordinates>78.4.*,17.3.* 78.2.*,17.5.* 78.7.*,17.1.* 78.3.*,17.3.* 78.5.*,17.2.*</coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>78.1.*,17.4.* 78.1.*,17.3.* 78.5.*,17.5.* 78.4.*,17.2.* 78.4.*,17.6.*</coordinates></LinearRing></innerBoundaryIs></Polygon>");
		YUITest.Assert.areSame(true, pattern.test(result));
    },
    "an array of overlays argument should succeed": function () {
        var position = new google.maps.LatLng(17.37, 78.22);
        var marker = new google.maps.Marker({ 'position': position });

        var ll1 = new google.maps.LatLng(17.37, 78.47);
        var ll2 = new google.maps.LatLng(17.55, 78.21);
        var ll3 = new google.maps.LatLng(17.15, 78.73);
        var line = new google.maps.Polyline({ 'path': [ll1, ll2, ll3] });

        var sll1 = new google.maps.LatLng(17.37, 78.47);
        var sll2 = new google.maps.LatLng(17.55, 78.21);
        var sll3 = new google.maps.LatLng(17.15, 78.73);
        var sll4 = new google.maps.LatLng(17.35, 78.33);
        var sll5 = new google.maps.LatLng(17.25, 78.56);

        var hll1 = new google.maps.LatLng(17.47, 78.17);
        var hll2 = new google.maps.LatLng(17.35, 78.11);
        var hll3 = new google.maps.LatLng(17.55, 78.53);
        var hll4 = new google.maps.LatLng(17.25, 78.43);
        var hll5 = new google.maps.LatLng(17.65, 78.46);

        var poly = new google.maps.Polygon({ 'paths': [[sll1, sll2, sll3, sll4, sll5], [hll1, hll2, hll3, hll4, hll5]] });

        result = this.target.getOverlayKmlGeometryString([marker,line,poly]);
        var pattern = new RegExp(".*<Polygon><outerBoundaryIs><LinearRing><coordinates>78.4.*,17.3.* 78.2.*,17.5.* 78.7.*,17.1.* 78.3.*,17.3.* 78.5.*,17.2.*</coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>78.1.*,17.4.* 78.1.*,17.3.* 78.5.*,17.5.* 78.4.*,17.2.* 78.4.*,17.6.*</coordinates></LinearRing></innerBoundaryIs></Polygon>.*");
		YUITest.Assert.areSame(true, pattern.test(result));
		pattern = new RegExp(".*<LineString><coordinates>78.47.*,17.37.* 78.21.*,17.55.* 78.73.*,17.15.*</coordinates></LineString>.*");
		YUITest.Assert.areSame(true, pattern.test(result));
		pattern = new RegExp(".*<Point><coordinates>78.22.*,17.37.*</coordinates></Point>.*");
		YUITest.Assert.areSame(true, pattern.test(result));
    }
});


var suite = new YUITest.TestSuite("g2kml Test Suite");
suite.add(isArray_TestCase);
suite.add(getPointKmlGeometryString_TestCase);
suite.add(getLineKmlGeometryString_TestCase);
suite.add(getPolygonKmlGeometryString_TestCase);
suite.add(getOverlayKmlGeometryString_TestCase);

//create the console
var r = new Y.Console({
    newestOnTop: false,
    style: 'block' // to anchor in the example content
});

r.render('#testLogger');

var TestRunner = YUITest.TestRunner;

TestRunner.add(suite);


//function to handle events generated by the testrunner
function logEvent(event) {

    //data variables
    var message = "",
            messageType = "";

    switch (event.type) {
        case TestRunner.BEGIN_EVENT:
            message = "Testing began at " + (new Date()).toString() + ".";
            messageType = "info";
            break;

        case TestRunner.COMPLETE_EVENT:
            message = Y.substitute("Testing completed at " +
                    (new Date()).toString() + ".\n" +
                    "Passed:{passed} Failed:{failed} " +
                    "Total:{total} ({ignored} ignored)",
                    event.results);
            messageType = "info";
            break;

        case TestRunner.TEST_FAIL_EVENT:
            message = event.testName + ": failed.\n" + event.error.getMessage();
            messageType = "fail";
            break;

        case TestRunner.TEST_IGNORE_EVENT:
            message = event.testName + ": ignored.";
            messageType = "ignore";
            break;

        case TestRunner.TEST_PASS_EVENT:
            message = event.testName + ": passed.";
            messageType = "pass";
            break;

        case TestRunner.TEST_SUITE_BEGIN_EVENT:
            message = "Test suite \"" + event.testSuite.name + "\" started.";
            messageType = "info";
            break;

        case TestRunner.TEST_SUITE_COMPLETE_EVENT:
            message = Y.substitute("Test suite \"" +
                    event.testSuite.name + "\" completed" + ".\n" +
                    "Passed:{passed} Failed:{failed} " +
                    "Total:{total} ({ignored} ignored)",
                    event.results);
            messageType = "info";
            break;

        case TestRunner.TEST_CASE_BEGIN_EVENT:
            message = "Test case \"" + event.testCase.name + "\" started.";
            messageType = "info";
            break;

        case TestRunner.TEST_CASE_COMPLETE_EVENT:
            message = Y.substitute("Test case \"" +
                    event.testCase.name + "\" completed.\n" +
                    "Passed:{passed} Failed:{failed} " +
                    "Total:{total} ({ignored} ignored)",
                    event.results);
            messageType = "info";
            break;
        default:
            message = "Unexpected event " + event.type;
            message = "info";
    }

    //only log if required
    Y.log(message, messageType, "TestRunner");
}

//listen for events to publish to the logger
TestRunner.attach(TestRunner.BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_CASE_BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_CASE_COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_SUITE_BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_SUITE_COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_PASS_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_FAIL_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_IGNORE_EVENT, logEvent);


//run the tests
TestRunner.run();

});
        </script>
    </body>
</html>
